Skip to main content

Data Vectors

The Plane

Pairs of numbers can be depicted as points on a plane.

The plane is normally denoted by R2\mathbb{R}^2.

Details

Pairs of numbers can be depicted as points on a plane.

Definition

A plane is a perfectly flat surface with no thickness and no end, it can extend forever in all directions. It has two-dimensions, length and width. We need two values to find a point on the plane.

Normally we talk about "the plane" (or "the xyxy-plane") as the collection of all pairs of numbers and denote it by

R2={(x,y):x,yR},\mathbb{R}^2 = \{ (x,y) : x,y \in \mathbb{R} \},

giving coordinates to each point. The plane is also sometimes called The Cartesian coordinate system, named after its inventor, the French polymath René Descartes.

Examples

Example

Plotting the point (2,4)(2,4) in the xyxy-plane using R:

plot(2,4,xlim=c(0,6),ylim=c(0,6),xlab="x",ylab="y",cex=2)
text(2,4,"(2,4)",pos=4,cex=2)

Additional points can be added using the points() function:

points(3,5, cex = 0.5) ## a point at (3,5)

If you have two sets of coordinates on a plane you, can calculate the distance between the two points and graph the line connecting the points.

Example

What is the distance between the two points (3,9)(3,9) and (5,1)(5,1)? What is the distance between the 2 points (3,9) and (5,1)?

We will use the Pythagorean theorem:

d=(x2x1)2+(y2y1)2d = \sqrt{(x_{2}-x_{1})^{2}+(y_{2}-y_{1})^{2}}

We insert our values into the formula:

d=(53)2+(19)2d=\sqrt{(5-3)^{2}+(1-9)^{2}}

When we combine inside the parentheses we get:

d=(2)2+(8)2d=\sqrt{(2)^{2}+(-8)^{2}}

Squaring both terms:

d=4+64d=\sqrt{4+64}

Then we take the square root:

d=68d=\sqrt{68}

The result:

d8.2462d \approx 8.2462

Simple Plots in R

Graphing functions in R

  • plot() - plots a scatter plot (as a line plot)

  • points() - adds points to a plot

  • text() - adds text to a plot

  • lines() - adds lines to a plot

Fig. 3

Figure: Points on a plane, drawn with R.

Examples

Example
plot(2,3)

gives a single plot and

plot(2,3, xlim=c(0,5), ylim=c(0,5))

gives a single plot but forces both axes to range from 0 to 5.

Example

The following R commands can be used to generate a plot with two points:

plot(1,2,xlim=c(0,5),ylim=c(0,5),xlab="x",ylab="y")
points(3,1)
text(1,2,"(1,2)",pos=4, cex=2)
text(3,1,"(3,1)",pos=4, cex=2)
Example

In this example, we plot three points. The first two arguments of the plot function. The third plot was added with the points are by including vectors with a length of 22 as the xx and yy arguments of the plot function. The third point was added with the points function. The second and third points were labeled using the text function and a line was drawn between them using the lines function.

plot(c(2,3),c(3,4),xlim=c(2,6),ylim=c(1,5),xlab="x",ylab="y")
points(4,2)
text(3,4,"(3,4)",pos=4, cex=2)
text(4,2,"(4,2)",pos=4, cex=2)
lines(c(3,4), c(4,2))

Note: Note that if you are unsure of what format the arguments of an R function needs to be, you can call a help file by typing ? before the function name (e.g. ?lines).

Data

Data are usually a sequence of numbers, typically called a vector.

Details

When we collect data these are one or more sequences of numbers, collected into data vectors. We commonly think of these data vectors as columns in a table.

Examples

Example

In R, if the command

x <- c(4,5,3,7)

is given, then x contains a vector of numbers.

Example

Create a function in R, give it a name Myfunction which takes the sum of x and y:

Myfunction <- function(x,y) { sum(x,y) }

If you input the vectors 1:3 and 4:7 into the function it will calculate the sum of x <- (1+2+3) and y <- (4+5+6+7) as follows:

> Myfunction(1:3,4:7)
[1] 28

Indices for a Data Vector

If data are in a vector x, then we use indices to refer to individual elements.

Details

If i is an integer then xix_i denotes the ithi^{th} element of xx.

Note: Although we do not distinguish (much) between row- and column vectors, usually a vector is thought of as a column. If we need to specify the type of vector, row or column, then for vector xx, the column vector would be referred to as xx' and the row vector as xTx^T.

(the transpose of the original).

Examples

Example

If x=(4,5,3,7)x=(4,5,3,7) then x1=4x_1=4 and x4=7x_4=7

Example

How to remove all indices below a certain value in R?

> x <- c(1,5,8,9,4,16,12,7,11)

> x
[1] 1 5 8 9 4 16 12 7 11

> y <- x[x>10]

> y
[1] 16 12 11
Example

Consider a function that takes to vectors

aRn,bNma \in \mathbb{R}^n, b \in \mathbb{N}^m

as arguments with:

nmn \ge m

and:

1b1,,bmn.1 \le b_1,\dots,b_m \le n.

The function returns the sum:

i=1mabi\sum_{i = 1}^m {a_b}_i

Long version:

> fn <- function(a,b) {
+ result <- sum(a[b])
+ return(result)
+ }

Short version:

fN <- function(a,b) sum(a[b])

Summation

We use the symbol Σ\Sigma to denote sums.

In R, the sum function adds numbers.

Examples

Example

If x=(4,5,3,7)x=(4,5,3,7)

then

i=14xi=x1+x2+x3+x4=4+5+3+7=19\sum_{i=1}^{4} x_i = x_1+x_2+x_3+x_4 = 4+5+3+7 = 19

and

i=24xi=x2+x3+x4=5+3+7=15.\sum_{i=2}^{4} x_i = x_2+x_3+x_4 = 5+3+7 = 15.

In R one can give the corresponding commands:

> x <- c(4,5,3,7)

> x
[1] 4 5 3 7

> sum(x)
[1] 19

> sum(x[2:4])
[1] 15